package com.uha.reggie.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.uha.reggie.util.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author uha
 * @date 2023/6/12
 * @apiNote 自动填充处理类
 *  https://blog.csdn.net/weixin_48444028/article/details/126461283  自动填充用户id
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    //在实体类中需要自动填充的字段上加注解

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill .......");

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill .......");

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        Long currentId = BaseContext.getCurrentId();

        log.info("******************************************************");
        log.info(String.valueOf(currentId));
        log.info("******************************************************");

    }
}
